<template>
    <div>
        <el-dialog center :before-close="close" :title="info.win ? '添加管理员' : '编辑管理员'" :visible.sync="info.isShow">
            <el-form :model="adminAdd" :rules="rules" ref="adminForm">
                <el-form-item label="角色编号" prop="roleid" :label-width="formLabelWidth">
                    <el-select v-model="adminAdd.roleid" placeholder="请选择">
                        <el-option v-for="item in roleList" :key="item.id" :label="item.rolename" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="用户名称" prop="username" :label-width="formLabelWidth">
                    <el-input v-model="adminAdd.username" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="密码" prop="password" :label-width="formLabelWidth">
                    <el-input type="password" v-model="adminAdd.password" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="状态" :label-width="formLabelWidth">
                    <el-switch v-model="adminAdd.status" active-color="#13ce66" inactive-color="#ff4949"
                        :active-value="1" :inactive-value="2">
                    </el-switch>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="close">取 消</el-button>
                <el-button type="primary" @click="confirm">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
// 引入封装好的接口
import { userAdd, userInfo, userEdit } from '../../request/api'
// 辅助性函数
import { mapGetters, mapActions } from 'vuex'
export default {
    props: ['info'],
    data() {
        return {
            formLabelWidth: '120px',
            adminAdd: {
                roleid: '',
                username: '',
                password: '',
                status: 1,
            },
            rules: {
                username: [
                    { required: true, message: '请输入管理员名称', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                roleid: [
                    { required: true, message: '请选择管理员角色', trigger: 'blur' }
                ],
            }
        };
    },
    computed: {
        // 角色列表
        ...mapGetters({
            roleList: "role/getRoleList"
        })
    },
    mounted() {
        // 如果角色列表长度为0 就调用接口， 
        if (this.roleList.length == 0) {
            this.roleListAction()
        }
    },
    methods: {
        lookup(uid) {
            // 调用查询接口
            userInfo({
                uid
            }).then(res => {
                console.log(res, '管理员查询');
                // 数据回显
                this.adminAdd = res.list
                // 增加uid属性，为修改信息使用
                this.adminAdd.uid = uid
                this.adminAdd.password = ""
            })
        },
        ...mapActions({
            // 角色action
            roleListAction: 'role/getRoleListAction',
            // 管理员列表
            adminListAction: 'admin/getAdminListAction',
            getAdminCountAction:'admin/getAdminCountAction'
        }),
        // 点击取消事件
        close() {
            this.$emit('close', false)
            // 清空校验, 重置
            this.$refs.adminForm.resetFields()
        },
        // 点击确认事件
        confirm() {
            this.$refs.adminForm.validate(flag => {
                console.log(flag);
                if (flag) {
                    console.log(this.adminAdd);
                    console.log('添加成功');
                    if (this.info.win) {  // win 为真是添加弹窗，为假是编辑弹窗
                        // 调用添加接口
                        userAdd(this.adminAdd)
                            .then(res => {
                                console.log(res, '管理员添加响应');
                                if (res.code == 200) {
                                    this.$message.success(res.msg)
                                    // 重置表单
                                    this.close()
                                    // 更新列表
                                    this.adminListAction()
                                    // 获取总条数
                                    this.getAdminCountAction()
                                } else {
                                    this.$message.error(res.msg)
                                }

                            })
                    } else {
                        // 调用修改接口
                        userEdit(this.adminAdd)
                            .then(res => {
                                console.log(res, '管理员编辑');
                                if (res.code == 200) {
                                    this.$message.success(res.msg)
                                    // 重置表单
                                    this.close()
                                    // 更新列表
                                    this.adminListAction()
                                } else {
                                    this.$message.error(res.msg)
                                }

                            })
                    }
                } else {
                    console.log('添加失败');
                }
            })
        }
    }
};
</script>

<style  lang="" scoped>

</style>
